import 'dart:math';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:saas_order_meal/model/order_request_data.dart';
import 'package:saas_order_meal/res/resources.dart';
import 'package:saas_order_meal/utils/meal_ext_utils.dart';

import '../saas_order_meal.dart';
import '../viewmodel/apply_meal_view_model.dart';

class ApplyMealPage extends BaseStatefulMvvmWidget {
  final OrderRequestData orderData;

  ApplyMealPage({required this.orderData});

  @override
  State<StatefulWidget> createState() => _ApplyMealState();
}

class _ApplyMealState extends BaseMvvmState<ApplyMealViewModel, ApplyMealPage> {
  @override
  Widget? buildLoadingContentView() {
    return Column(
      children: [
        SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  CommonText('下单信息', textSize: 18, fontWeight: FontWeight.bold),
                  SizedBox(
                    height: 12,
                  ),
                  CommonText('用餐日期：${widget.orderData.date}'),
                  SizedBox(
                    height: 12,
                  ),
                  CommonText("用餐人：${widget.orderData.elderName}"),
                ],
              ).addToContainer(
                  width: double.infinity,
                  margin: EdgeInsets.symmetric(horizontal: 16, vertical: 20),
                  decoration: BoxDecoration(
                      color: Colors.white, borderRadius: 10.borderRadius),
                  padding: EdgeInsets.symmetric(horizontal: 12, vertical: 16)),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  CommonText('菜品信息', textSize: 18, fontWeight: FontWeight.bold),
                  SizedBox(
                    height: 12,
                  ),
                  ...widget.orderData.selectFood.map((e) {
                    return Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            CommonText(
                                    "${e.date!.formatDate([
                                          'm',
                                          '月',
                                          'd',
                                          '日'
                                        ])}  ${e.timeSectionName}",
                                    textSize: 17,
                                    fontWeight: FontWeight.bold)
                                .setWeight(1),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.end,
                              children: [
                                CommonText('原价：¥${e.price.d}'),
                                if(viewModel.orderRequestData.limitMap!=null&&viewModel.orderRequestData.limitMap![e.timeSection]!=null)
                                  ...[
                                    SizedBox(height: 12,),
                                    CommonText('限价外金额：¥${max(e.price-(viewModel.orderRequestData.limitMap![e.timeSection] as num), 0).d}',
                                    textSize: 14,textColor: Colors.red)
                                  ]
                              ],
                            )
                          ],
                        ),
                        CommonText(e.selectFood
                            .map((e) => "${e.goodsName} x${e.number}")
                            .join("、"))
                      ],
                    ).addToContainer(margin: EdgeInsets.only(bottom: 12));
                  }).toList().insertWidget(Divider()),
                ],
              ).addToContainer(
                  width: double.infinity,
                  margin: EdgeInsets.symmetric(
                    horizontal: 16,
                  ),
                  decoration: BoxDecoration(
                      color: Colors.white, borderRadius: 10.borderRadius),
                  padding: EdgeInsets.symmetric(horizontal: 12, vertical: 16)),
            ],
          ),
        ),
        Container().setWeight(1),
        Row(
          children: [
            SizedBox(width: 20,),
            Row(
              children: [
                SizedBox(width: 20,),
                CommonText("合计：",textColor: Colors.white,fontWeight: FontWeight.bold,textSize: 19),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    CommonText("¥${viewModel.payPrice.d}",textColor: Colors.white,fontWeight: FontWeight.bold,textSize: 19),
                    if(viewModel.totalPrice!=viewModel.payPrice)
                      ...[
                        SizedBox(width: 8,),
                        Text(" ¥${viewModel.totalPrice.d}   ",style: TextStyle(fontSize: 12,color: Colors.white,decoration: TextDecoration.lineThrough,
                            decorationColor: Colors.white),)
                      ]
                  ],
                )
              ],
            ).addToContainer(
              height: 44,
              decoration: BoxDecoration(
                color: Color(0x90000000),
                borderRadius: BorderRadius.horizontal(left: 20.radius)
              )
            ).setWeight(1),
            TextButton(
              onPressed: () {
                viewModel.submitOrderClick();
              },
              child: CommonText('确认结算', textColor: Colors.white),
              style: ButtonStyle(
                  shape: MaterialStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.horizontal(right: 20.radius)
                  )),
                  backgroundColor:
                  MaterialStateProperty.all(SaasOrderMeal.mainColor),
                  minimumSize: MaterialStateProperty.all(
                      Size.fromHeight(44))),
            ).setWeight(1),
            SizedBox(width: 20,),
          ],
        ),
        SizedBox(
          height: 24,
        )
      ],
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Color(0xfff1f1f1),
      appBar: AppBar(
        title: Text("下单确认"),
      ),
      body: loadingContentWidget,
    );
  }

  @override
  ApplyMealViewModel createViewModel() {
    return ApplyMealViewModel(context, orderRequestData: widget.orderData);
  }

  @override
  void onRetryClick() {
    // TODO: implement onRetryClick
  }
}
